#include <stdio.h>
#include <simpio.h>
#include "genlib.h"
#include "strlib.h"
#include "scanadt.h"
#include "exp.h"
#include "symtab.h"
#include "eval.h"
#include "exception.h"
#include "command.h"
#include "parse.h"
#include <ctype.h>

static void initSavedCommands(string s[]);

main()
{
	scannerADT scanner;
	int value, buf;
	string line, savedCommands[MAX_SIZE], token;	

	printf(" ----- Minimal Basic -- type HELP for help -----\n\n");
	InitCommandTable();
	InitVariableTable();
	initSavedCommands(savedCommands);
	scanner = NewScanner();
	SetScannerSpaceOption(scanner, IgnoreSpaces);
	while (TRUE) 
	{
		try 
		{
			printf("=> ");
			line = ConvertToLowerCase(GetLine());
			SetScannerString(scanner, line);
			token = ReadToken(scanner);
			SetScannerString(scanner, line);
			except(ErrorException)
				printf("Error: %s\n", (string) GetExceptionValue());
		} endtry
		if (isdigit(line[0]))
		{
			buf = testIfInteger(scanner);
			if (buf == -1)
			{
				try
				{
					printf("Illegal adress\n");
					except(ANY)
						printf("Error: %s\n", (string) GetExceptionValue());
				}endtry;
			}
			savedCommands[buf] = "";
			if (!MoreTokensExist(scanner)) savedCommands[buf] = NULL;
			while (MoreTokensExist(scanner))
			{
				token = ReadToken(scanner);
				if(StringEqual(token, "run") || StringEqual(token, "list") || StringEqual(token, "quit") || StringEqual(token, "help"))
				{
					try 
					{
						printf("Illegal command %s after adress value\n", token);
						except(ANY)
							printf("Error: %s\n", (string) GetExceptionValue());
					} endtry
				}
				savedCommands[buf] = Concat(savedCommands[buf],token); 
				savedCommands[buf] = Concat(savedCommands[buf]," "); 
			}	
		}
		else
			ExecuteCommand(scanner, savedCommands);
		
	}
}

static void initSavedCommands(string s[])
{
	int i;

	for(i = 0; i <= MAX_SIZE; i++)
		s[i] = NULL;
}